'use strict';
const db = uniCloud.database();
const user = db.collection('Accounts')
exports.main = async (event, context) => {
    console.log('AddUser event:', event)

    if (!event.username || !event.password) {
        return {
            code: 400,
            message: '缺少必要字段'
        }
    }

    const userData = {
        username: event.username,
        password: event.password,
        nickname: event.nickname,
        mobile: event.mobile,
        email: event.email,
        createtime: Date.now()
    }

    try {
        const result = await user.add(userData)
        console.log('添加结果:', result)
        return {
            code: 200,
            data: {
                id: result.id
            },
            message: '添加成功'
        }
    } catch (error) {
        console.error('添加失败:', error)
        return {
            code: 500,
            message: '添加失败'
        }
    }
}